<template>
  <div>
    <el-dialog
      ref="table"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="配件选择"
      width="70%"
      @opened="searchHandle"
      @close="cancel" >
      <!-- 查询条件 -->
      <div class="searchform">
        <el-form
          ref="searchForm"
          :inline="true"
          :model="search"
          size="mini"
          label-width="100px"
          label-position="right"
        >
          <el-form-item
            prop="partCode"
            label="配件代码"
          >
            <el-input
              v-model="search.partCode"
              placeholder="配件代码"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="partName"
            label="配件名称"
          >
            <el-input
              v-model="search.partName"
              placeholder="配件名称"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="storageCode"
            label="仓库名称"
          >
            <r-select
              v-model="search.storageCode"
              url="/basedata/store/accessories/Warehouses"
              model="DMSCLOUD_PART"
              selectid="STORAGE_CODE"
              selectname="STORAGE_NAME"
            />
          </el-form-item>
          <div style="text-align:center">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="searchHandle"
              >查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                icon="el-icon-refresh"
                @click="resetForm('searchForm')"
              >重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <div class="table">
        <div class="tableHead">
          <div class="tabletitle">配件信息</div>
        </div>
        <BaseTable
          ref="repairtable"
          @chaneg-size="changeSizeHandle"
          @chaneg-page="changePageHandle"
          @row-dblclick="dblclick"
        >
          <el-table-column
            label="序号"
            type="index"
          />
          <el-table-column
            align="center"
            label="操作"
            min-width="100px"
          >
            <template slot-scope="scope">
              <el-button
                icon="el-icon-check"
                type="primary"
                @click="repairSelect(scope.row)"
              >选择</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="PART_CODE"
            label="配件代码"
          />
          <el-table-column
            prop="PART_NAME"
            label="配件名称"
          />
          <el-table-column
            prop="STORAGE_NAME"
            label="仓库名称"
          />
          <el-table-column
            prop="STORAGE_POSITION_CODE"
            label="库位"
          />
          <el-table-column
            prop="STOCK_QUANTITY"
            label="库存数量"
          />
          <el-table-column
            :formatter="filterCode"
            prop="PART_GROUP_CODE"
            label="配件类别"
            type="1300"
          />
          <el-table-column
            prop="SALES_PRICE"
            label="销售价"
          />
          <el-table-column
            prop="LIMIT_PRICE"
            label="销售限价"
          />
          <el-table-column
            prop="COST_PRICE"
            label="成本价"
          />
          <el-table-column
            :formatter="filterCode"
            prop="PART_STATUS"
            label="是否停用"
            type="1004"
          />
          <el-table-column
            prop="COST_PRICE"
            label="成本价"
          />
          <el-table-column
            prop="unitShow"
            label="计量单位"
          />
        </BaseTable>
      </div>
      <el-form ref="form" :rules="rules" :model="form" :inline="true" label-position="right" label-width="120px" size="mini" >
        <el-form-item label="仓库名称" prop="storageName" ><r-select v-model="form.storageName" disabled="disabled" url="/basedata/store/accessories/Warehouses" model="DMSCLOUD_PART" selectid="STORAGE_CODE" selectname="STORAGE_NAME" class="form-input" /></el-form-item>
        <el-form-item label="配件代码" prop="partNo" ><el-input v-model="form.partNo" disabled="disabled" class="form-input" /></el-form-item>
        <el-form-item label="配件名称" prop="partName" ><el-input v-model="form.partName" disabled="disabled" class="form-input" /></el-form-item>
        <el-form-item label="数量" prop="partQuautityNum" >
          <el-input v-model="form.partQuautityNum" class="form-input" />
        </el-form-item>
        <el-form-item label="价格类型" prop="priceTypeShow" >
          <r-select v-model="form.priceTypeShow" type="1311" exclude-code="13111003,13111005,13111006,13111008" class="form-input" @change="handelPriceType" />
        </el-form-item>
        <el-form-item label="价格系数" prop="priceTieShow" >
          <el-input v-model="form.priceTieShow" class="form-input" />
        </el-form-item>
        <el-form-item label="发料单价" prop="partSalesPrice" >
          <el-input v-model="form.partSalesPrice" class="form-input" />
          <span v-show="false">
            {{ form.partSalesAmount = isNaN(form.priceTieShow*form.partSalesPrice*form.partQuautityNum)?0:(form.priceTieShow*form.partSalesPrice*form.partQuautityNum).toFixed(2) }}
          </span>
        </el-form-item>
        <el-form-item label="销售金额" prop="partSalesAmount" >
          <el-input v-model="form.partSalesAmount" disabled="disabled" class="form-input" />
        </el-form-item>
        <el-form-item label="维修项目" prop="labourCode" >
          <el-select v-model="form.labourCode" placeholder="维修项目" class="form-input">
            <el-option v-for="role in dataItems" :key="role.labourCode" :label="role.labourName" :value="role.labourCode"/>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="resetProject" >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListPart } from '@/api/repair/order/appointment'
import BaseTable from '@/components/BaseTable'
import rSelect from '@/components/Select/Select'
import formMixins from '@/mixins/formMixins'
import Pagination from '@/components/Pagination'
import {
  maxPrecision, moneyMinus
} from '@/utils/validator'

export default {
  name: 'AddPart',
  components: {
    BaseTable,
    rSelect,
    Pagination
  },
  mixins: [formMixins],
  props: {
    dataItem: Array,
    dataPart: Array
  },
  data() {
    return {
      dataItems: this.$props.dataItem,
      form: {},
      dialogFormVisible: true,
      multipleSelection: [],
      search: {
        limit: 10,
        pageNum: 1,
        storageCode: ''
      },
      loading: false,
      searchPart: {
        limit: 10,
        pageNum: 1
      },
      partTableData: [],
      rules: {
        partQuautityNum: [{ required: true, message: '必填项', trigger: 'blur' }, { trigger: 'blur', validator: maxPrecision, limit: 2 }],
        partSalesPrice: [{ required: true, message: '必填项', trigger: 'blur' }, { trigger: 'blur', validator: moneyMinus }],
        labourCode: [{ required: true, message: '必填项', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  updated() {
    this.searchHandle()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    resetProject() {
      if (this.form.LIMIT_PRICE < this.form.partSalesPrice) {
        this.$message({ type: 'warning', message: '配件出库单价不能超过销售限价' })
        return
      }
      var repairPro = this.form
      var isDouble = false
      var lastRepairPro = this.$props.dataPart
      if (lastRepairPro.length > 0) {
        var currentLabour = repairPro.labourCode + '|' + repairPro.partNo
        for (var i = 0; i < lastRepairPro.length; i++) {
          var lastLabour = lastRepairPro[i].labourCode + '|' + lastRepairPro[i].partNo
          if (lastLabour === currentLabour) {
            isDouble = true
          }
        }
      }
      if (isDouble) {
        this.$message({
          type: 'info',
          message: '重复添加'
        })
      } else {
        this.confirmHandle()
      }
    },
    confirmHandle() {
      this.$refs.form.validate(flag => {
        if (flag) {
          this.$set(this.form, 'discount', 1)
          this.$props.dataItem.forEach(el => {
            if (this.form.labourCode === el.labourCode) {
              this.$set(this.form, 'orderNum', el.orderNum)
              this.$set(this.form, 'labourName', el.labourName)
              this.$set(this.form, 'chargePartitionCode', el.chargePartitionCode)
            }
          })
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.$emit('LaPart', [this.form])
        }
      })
    },
    // 改变当前也显示数目
    changeSizeHandlePart(table, size) {
      this.searchPart.limit = size
      // this.subgroups(this.labourdata);
    },
    // 改变页数
    changePageHandlePart(table, num) {
      this.searchPart.pageNum = num
      // this.subgroups(this.labourdata);
    },
    handleSizeChangePart(size) {
      this.$emit('chaneg-size', this, size)
    },
    handleCurrentChangePart(current) {
      this.$emit('chaneg-page', this, current)
    },
    handelPriceType(val) {
      const code = (val + '').slice(-1)
      switch (code) {
        case '1':
          // 销售价  SALES_PRICE
          this.form.partSalesPrice =
            (this.form.SALES_PRICE || 0).toFixed(2) * 1
          break
        case '2':
          // 建议  ADVICE_SALE_PRICE
          this.form.partSalesPrice =
            (this.form.ADVICE_SALE_PRICE || 0).toFixed(2) * 1
          break
        case '7':
          // 网点价 SALES_PRICE
          this.form.partSalesPrice =
            (this.form.SALES_PRICE || 0).toFixed(2) * 1
          break
        case '4':
          // 销售限价  LIMIT_PRICE
          this.form.partSalesPrice =
            (this.form.LIMIT_PRICE || 10000).toFixed(2) * 1
          break
      }
    },
    // 获取配件选择信息
    searchHandle() {
      const table = this.$refs['repairtable']
      getListPart(this.search)
        .then(res => {
          table.complete().filData(res)
        })
        .catch(err => {
          // table.complete();
        })
    },
    // 双击维修项目选择
    dblclick(row) {
      this.$set(row, 'storageCode', row.STORAGE_CODE)
      this.$set(row, 'storageName', row.STORAGE_NAME)
      this.$set(row, 'partNo', row.PART_CODE)
      this.$set(row, 'partName', row.PART_NAME)
      this.$set(row, 'partQuantity', 1)
      this.$set(row, 'priceTieShow', 1)
      this.$set(row, 'priceTypeShow', 13111001)
      this.$set(row, 'partSalesPrice', row.SALES_PRICE)
      this.form = row
    },
    // 维修项目选择,双击
    repairSelect(data) {
      this.dblclick(data)
    },
    cancel() {
      this.$emit('cancel1', 1)
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
</style>
